#include "LabeledNode.h"

/**
 *  @fn	LabeledNode::LabeledNode(const SyntaxTreeNode *labelsNode)
 * 
 *  @brief	Constructor. 
 * 
 *  @author	Nikola Novak
 *  @date	6/24/2009
 * 
 *  @param	labelsNode	If non-null, the labels node.
 */
LabeledNode::LabeledNode(const SyntaxTreeNode *labelNode) {
	for (int x = 0; x < labelNode->Nodes->GetCount(); ++x) {
		if (labelNode->Nodes->GetData(x)->Production == Gr_BlockLabel__NAME_spPer_NAME_spColon) {
			std::ostringstream lbl;
			lbl << CompilerData->Units->GetLexicUnitString(&CompilerData->Units->GetLexicUnit(labelNode->Nodes->GetData(x)->Nodes->GetData(0)->LexUnitID));
			lbl << ".";
			lbl << CompilerData->Units->GetLexicUnitString(&CompilerData->Units->GetLexicUnit(labelNode->Nodes->GetData(x)->Nodes->GetData(2)->LexUnitID));
			labels.push_back(lbl.str());
		} else if (labelNode->Nodes->GetData(x)->Production == Gr_BlockLabel__NAME_spColon) {
			labels.push_back(CompilerData->Units->GetLexicUnitString(&CompilerData->Units->GetLexicUnit(labelNode->Nodes->GetData(x)->Nodes->GetData(0)->LexUnitID)));
		} else { /* Cannot happen. */ }
	}
}
/**
 *  @fn	std::vector<std::string> LabeledNode::getLabels(void)
 * 
 *  @brief	Gets the labels.
 * 
 *  @author	Nikola Novak
 *  @date	6/24/2009
 * 
 *  @return	The labels.
 */
std::vector<std::string> LabeledNode::getLabels(void) {
	return labels;
}
/**
 *  @fn	bool LabeledNode::hasLabel(std::string labelText)
 * 
 *  @brief	Query if 'labelText' has label. 
 * 
 *  @author	Nikola Novak
 *  @date	6/24/2009
 * 
 *  @param	labelText	The label text.
 * 
 *  @return	true if label, false if not.
 */
bool LabeledNode::hasLabel(std::string labelText) {
	for (std::vector<std::string>::iterator i = labels.begin(); i != labels.end(); ++i) {
		if ((*i) == labelText) return true;
	}
	return false;
}
/**
 *  @fn	std::string LabeledNode::getLabelsDebugString(void)
 * 
 *  @brief	Gets the labels debug string. 
 * 
 *  @author	Nikola Novak
 *  @date	6/24/2009
 * 
 *  @return	The labels debug string.
 */
std::string LabeledNode::getLabelsDebugString(void) {
	std::ostringstream res;
	for (std::vector<std::string>::iterator i = labels.begin(); i != labels.end(); ) {
		res << *i;
		if (++i != labels.end()) res << ", ";
	}
	return res.str();
}
